package com.yiren.backstage.judge.services.standard.impl;

import android.content.Context;

import com.yiren.backstage.config.enums.SiteConfigCodeEnum;
import com.yiren.backstage.currency.bean.GpsInfoBean;
import com.yiren.backstage.currency.bean.LinePointBean;
import com.yiren.backstage.currency.constant.SignalGConstant;
import com.yiren.backstage.currency.utils.GpsUtil;
import com.yiren.backstage.judge.services.standard.IAutoPlayService;

import java.util.List;

/**
 * Time:2022/3/10 16:29
 * Author:xiaozan
 * Description:自动播报线程服务
 */
public class AutoPlayServiceImpl implements IAutoPlayService {

    private Context context;

    public AutoPlayServiceImpl(Context context) {
        this.context = context;
    }

    /**
     * 是否播报线路点位
     *
     * @param realGpsInfo      实时gps对象
     * @param currentLinePoint 当前线路点位对象
     * @return
     */
    public boolean isPlayLinePoint(GpsInfoBean realGpsInfo, LinePointBean currentLinePoint) {
        // 距离点位多少距离  触发点位
        float gpsPrecision = (float) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L13.toString());
       /* //采集点位的车速与当前车速对比，如果采集时的车速低于当前车速，那么GPS精准度距离加大，反之减小 做一个补偿
        float currentSpeed = realGpsInfo.getSpeed();
        float collectionSpeed = currentLinePoint.getGpsInfo().getSpeed();
        if (currentSpeed > collectionSpeed) {
            //增加GPS点位精准度距离
            float speedDifferebce = currentSpeed - collectionSpeed;
            gpsPrecision += speedDifferebce * 0.7;
        } else {
            //减小GPS点位精准度距离
            float speedDifferebce = collectionSpeed - currentSpeed;
            gpsPrecision -= speedDifferebce * 0.7;
        }*/
        // 方位匹配
        boolean bearingMatch = GpsUtil.bearingMatch(realGpsInfo, currentLinePoint, 40);
        // 点位距离匹配
        boolean distanceMatch = GpsUtil.getDistance(currentLinePoint, realGpsInfo) <= gpsPrecision;
        if (bearingMatch && distanceMatch) {
            return true;
        }
        return false;
    }

    /**
     * 释放已执行点位
     *
     * @param executePoints 已执行点位集合
     * @param realGpsInfo   当前gpsinfo对象
     */
    public LinePointBean releaseLinePoint(List<LinePointBean> executePoints, GpsInfoBean realGpsInfo) {
        for (LinePointBean linePoint : executePoints) {
            // 目标点距离
            double distance = GpsUtil.getDistance(linePoint, realGpsInfo);
            // 判断是否大于误差点
            boolean distanceGreaterMatch = distance > 30;
            //点位播报15秒后，才释放点位
            boolean timeGreaterMatch = System.currentTimeMillis() - linePoint.getPointPlayTime() > 10000;
            if (distanceGreaterMatch && timeGreaterMatch) {
                return linePoint;
            }
        }
        return null;
    }

    /**
     * 当前点位是否释放
     *
     * @param executePoints    已执行点位集合
     * @param currentLinePoint 当前点位
     * @return
     */
    public boolean isRelease(List<LinePointBean> executePoints, LinePointBean currentLinePoint) {
        try {
            for (int i = 0; i < executePoints.size(); i++) {
                LinePointBean linePoint = executePoints.get(i);
                if (linePoint.getLinePointId().equals(currentLinePoint.getLinePointId())) {
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
